﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="100px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ActivityName_FormItem"
								prop="Entity.ActivityName"
								:rules="[{ required: true, message: '活动名称为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="活动名称" tooltip="活动名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ActivityName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_StartTime_FormItem"
								prop="Entity.StartTime"
								:rules="[{ required: true, message: '开始时间为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="开始时间" tooltip="活动开始时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.StartTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_EndTime_FormItem"
								prop="Entity.EndTime"
								:rules="[{ required: true, message: '结束时间为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="结束时间" tooltip="活动结束时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.EndTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ActivityStatus_FormItem"
								prop="Entity.ActivityStatus"
								:rules="[{ required: true, message: '活动状态为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="活动状态" tooltip="活动状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.ActivityStatus" clearable class="width100">
									<el-option key="进行中" value="进行中" label="进行中"></el-option>
									<el-option key="未开始" value="未开始" label="未开始"></el-option>
									<el-option key="已结束" value="已结束" label="已结束"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_MaxJoinCount_FormItem"
								prop="Entity.MaxJoinCount"
								:rules="[{ required: true, message: '允许参加次数为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="允许参加次数" tooltip="允许参加次数" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.MaxJoinCount" :min="1" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div style="text-align: right">
					<WtmButton @click="onSubmit" type="primary" :button-text="$t('message._system.common.vm.submit')" style="margin-top: 15px" />
					<WtmButton @click="onClose" type="default" :button-text="$t('message._system.common.vm.cancel')" style="margin-top: 15px" />
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { SysActivityListApi as api } from '/@/views/activity/sysactivitylist/api';
import other from '/@/utils/other';
import { useRouter } from 'vue-router';
import { ISysActivityListVm } from '/@/views/activity/sysactivitylist/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysActivityListVm>>({
	vmModel: {
		Entity: {
			ID: null,
			ActivityName: null,
			StartTime: null,
			EndTime: null,
			ActivityStatus: null,
			MaxJoinCount: null,
		},
	},
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.edit(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
	} else if (useRouter().currentRoute.value.query.id) {
		stateData.vmModel.Entity.ID = useRouter().currentRoute.value.query.id as any;
	}
	const promises: Promise<any>[] = [
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((data: any) => other.setValue(stateData.vmModel, data)),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('refresh');
	emit('closeDialog');
};
</script>
